/*
 * CommitmentTask.java   0.0.1    3 Nov 2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.transactions.management.wrm;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.mentalsmash.documentation.Concurrency;
import org.mentalsmash.tazio.transactions.RollbackException;
import org.mentalsmash.tazio.transactions.TransactionStatus;
import org.mentalsmash.tazio.transactions.management.CommitmentAction;

import com.google.common.base.Preconditions;

/**
 * The <tt>CommitmentTask</tt> class represent the asynchronous computation of
 * commitment.
 * <p>
 * It is internally used by {@link TransactionManager}s to perform the
 * commitment of transactions, and it is up to them to possibly synchornized
 * over them. Exceptions thrown while performing this task (such as
 * {@link RollbackException}), are retrieved when calling the {@link #get()}
 * method, and are wrapped by an {@link ExecutionException}. <br />
 * If the commit is performed without problems this method should always return
 * {@link TransactionStatus#COMMITTED}.
 * </p>
 * <p>
 * Once a <tt>CommitmentTask</tt> is submitted to an {@link Executor} it cannot
 * be cancelled. In fact, calling {@link #cancel(boolean)} always throws an
 * {@link UnsupportedOperationException}.
 * </p>
 * 
 * @author Andrea Reale
 * @version 0.0.1
 * @see FutureTask
 */

@Concurrency(concurrentAccess = false, specification = "completely final status + this class should be accessed "
	+ "only a thread at a time. No concurrency concerns")
public class WRMCommitmentTask extends FutureTask<TransactionStatus> {

    private final WRMTransactionManager tm;

    /**
     * Create a new commitment task an associate to it the transaction to commit
     * 
     * @param toCommit
     *            the {@link TazioTransactionImpl} to commit
     */
    public WRMCommitmentTask(final CommitmentAction action,
	    final WRMTransactionManager tm) {
	super(action);
	Preconditions.checkNotNull(tm);
	this.tm = tm;
    }

    // private CommitmentTask(Callable<TransactionStatus> callable) {
    // super(callable);
    // }

    @Override
    public TransactionStatus get() throws ExecutionException,
	    InterruptedException {
	final TransactionStatus toReturn = super.get();
	tm.clearThreadTransaction();
	return toReturn;
    }

    @Override
    public TransactionStatus get(final long timeout, final TimeUnit unit)
	    throws ExecutionException, InterruptedException, TimeoutException {
	final TransactionStatus toReturn = super.get(timeout, unit);
	tm.clearThreadTransaction();
	return toReturn;
    }

    /**
     * Always throws an {@link UnsupportedOperationException}
     */
    @Override
    public boolean cancel(final boolean mayInterruptIfRunning) {
	throw new UnsupportedOperationException("Cannot cancel committments");
    }

}
